package com.sdry.service.impl.hpf;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sdry.mapper.hpf.AllReportMapper;
import com.sdry.mapper.hpf.PickApplyMapper;
import com.sdry.mapper.hpf.ProductMapper;
import com.sdry.model.hpf.Product;
import com.sdry.model.hpf.ProductRecord;
import com.sdry.model.hpf.ProductStatus;
import com.sdry.model.hpf.ProductionStatistics;
import com.sdry.model.hpf.ProductionStatisticsDetail;
import com.sdry.model.hpf.StationStatus;
import com.sdry.model.jyy.Cut;
import com.sdry.model.jyy.Shedudetail;
import com.sdry.model.jyy.Solid;
import com.sdry.model.workplace.Assemble;
import com.sdry.model.workplace.Centrifugal;
import com.sdry.model.workplace.MousseBundleSample;
import com.sdry.service.hpf.AllReportService;

/**
 * @ClassName: AllReportServiceImpl 
 * @Description:所有报表Service实现
 * @Author hpf
 * @Date 2018年11月28日 下午2:29:01 
 * @version 1.0
 */
@Service
public class AllReportServiceImpl implements AllReportService {

	@Resource
	private AllReportMapper allReportMapper;
	@Resource
	private ProductMapper productMapper;
	@Resource
	private PickApplyMapper pickApplyMapper;
	
	/**
	 * 查询生产记录报表总数
	 * @param map 条件
	 * @return
	 */
	public Integer countCutByTime(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return allReportMapper.countCutByTime(map);
	}
	
	/**
	 * 查询生产记录报表
	 * @param map 条件
	 * @return
	 */
	public List<ProductRecord> listAllCutByTime(Map<String, Object> map) {
		// TODO Auto-generated method stub
		List<ProductRecord> productRecordList = new ArrayList<>();
		List<Cut> cutList = allReportMapper.listAllCutByTime(map);
		for (int i = 0; i < cutList.size(); i++) {
			Cut cut = cutList.get(i);
			Product product = productMapper.findProductByPnum(cut.getCtube().substring(3, 7));
			ProductRecord productRecord = new ProductRecord();
			productRecord.setCremark(cut.getCremark());
			productRecord.setPname(product.getPname());
			productRecord.setPnum(product.getPnum());
			productRecord.setPmodel(product.getPcar());
			productRecord.setPunit(product.getPqf());
			productRecord.setPnorm(product.getClassify());
			productRecordList.add(productRecord);
		}
		return productRecordList;
	}

	/**
	 * 查询生产现况看板
	 * @return
	 */
	public List<StationStatus> listAllProductStatusByTime() {
		// TODO Auto-generated method stub
		//构造方法的字符格式这里如果小数不足2位,会以0补足
		DecimalFormat decimalFormat=new DecimalFormat(".00");
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateNowStr = sdf.format(d);
		Shedudetail shedudetail = pickApplyMapper.findShedudetailByDate(dateNowStr);
		List<StationStatus> stationStatusList = new ArrayList<>();
		String[] stationName = {"膜丝", "组装", "离心", "固化", "切割"};
		if (null != shedudetail) {
			Map<String, Object> map = new HashMap<>();
			map.put("beginTime", dateNowStr+" 00:00:00");
			map.put("endTime", dateNowStr+" 23:59:59");
			List<MousseBundleSample> msList = allReportMapper.listAllMSByTime(map);
			List<Assemble> zzList = allReportMapper.listAllZZByTime(map);
			List<Centrifugal> lxList = allReportMapper.listAllLXByTime(map);
			List<Solid> ghList = allReportMapper.listAllGHByTime(map);
			List<Cut> qgList = allReportMapper.listAllCutByTime(map);
			String b1 = shedudetail.getB1();//0960T 个数
			String c1 = shedudetail.getC1();//0980T	个数
			String d1 = shedudetail.getD1();//1080C	个数
			String e1 = shedudetail.getE1();//1060B 个数
			//计算膜丝工位每个产品的生产状况
			StationStatus stationStatus1 = new StationStatus();
			stationStatus1.setStationName(stationName[0]);
			List<ProductStatus> productStatusList1 = new ArrayList<>();
			ProductStatus productStatus11 = new ProductStatus();
			productStatus11.setPname("0960外压组件");
			int ms1 = 0;
			for (int i = 0; i < msList.size(); i++) {
				MousseBundleSample mousseBundleSample = msList.get(i);
				if ("01".equals(mousseBundleSample.getMbscode().substring(2, 4))) {
					ms1 ++;
				}
			}
			if ("".equals(b1) || b1 == null) {
				productStatus11.setPlanAmount(0);
				productStatus11.setLastPlanAmount(0 - ms1);
				productStatus11.setProductivity(0+"");
			} else {
				productStatus11.setPlanAmount((Integer.parseInt(b1)) * 24);
				productStatus11.setLastPlanAmount(((Integer.parseInt(b1)) * 24) - ms1);
				productStatus11.setProductivity(decimalFormat.format((double)ms1*100/(Integer.parseInt(b1) * 24))+"%");
			}
			productStatus11.setCompleteAmount(ms1);
			productStatusList1.add(productStatus11);
			ProductStatus productStatus12 = new ProductStatus();
			productStatus12.setPname("0980外压组件");
			int ms2 = 0;
			for (int i = 0; i < msList.size(); i++) {
				MousseBundleSample mousseBundleSample = msList.get(i);
				if ("02".equals(mousseBundleSample.getMbscode().substring(2, 4))) {
					ms2 ++;
				}
			}
			if ("".equals(c1) || c1 == null) {
				productStatus12.setPlanAmount(0);
				productStatus12.setLastPlanAmount(0 - ms2);
				productStatus12.setProductivity(0+"");
			} else {
				productStatus12.setPlanAmount((Integer.parseInt(c1)) * 24);
				productStatus12.setLastPlanAmount(((Integer.parseInt(c1)) * 24) - ms2);
				productStatus12.setProductivity(decimalFormat.format((double)ms2*100/(Integer.parseInt(c1) * 24))+"%");
			}
			productStatus12.setCompleteAmount(ms2);
			productStatusList1.add(productStatus12);
			ProductStatus productStatus13 = new ProductStatus();
			productStatus13.setPname("1080组件");
			int ms3 = 0;
			for (int i = 0; i < msList.size(); i++) {
				MousseBundleSample mousseBundleSample = msList.get(i);
				if ("04".equals(mousseBundleSample.getMbscode().substring(2, 4))) {
					ms3 ++;
				}
			}
			if ("".equals(d1) || d1 == null) {
				productStatus13.setPlanAmount(0);
				productStatus13.setLastPlanAmount(0 - ms3);
				productStatus13.setProductivity(0+"");
			} else {
				productStatus13.setPlanAmount((Integer.parseInt(d1)) * 32);
				productStatus13.setLastPlanAmount(((Integer.parseInt(d1)) * 32) - ms3);
				productStatus13.setProductivity(decimalFormat.format((double)ms3*100/(Integer.parseInt(d1) * 32))+"%");
			}
			productStatus13.setCompleteAmount(ms3);
			productStatusList1.add(productStatus13);
			ProductStatus productStatus14 = new ProductStatus();
			productStatus14.setPname("1060组件");
			int ms4 = 0;
			for (int i = 0; i < msList.size(); i++) {
				MousseBundleSample mousseBundleSample = msList.get(i);
				if ("03".equals(mousseBundleSample.getMbscode().substring(2, 4))) {
					ms4 ++;
				}
			}
			if ("".equals(e1) || e1 == null) {
				productStatus14.setPlanAmount(0);
				productStatus14.setLastPlanAmount(0 - ms4);
				productStatus14.setProductivity(0+"");
			} else {
				productStatus14.setPlanAmount((Integer.parseInt(e1)) * 32);
				productStatus14.setLastPlanAmount(((Integer.parseInt(e1)) * 32) - ms4);
				productStatus14.setProductivity(decimalFormat.format((double)ms4*100/(Integer.parseInt(e1) * 32))+"%");
			}
			productStatus14.setCompleteAmount(ms4);
			productStatusList1.add(productStatus14);
			stationStatus1.setProductStatusList(productStatusList1);
			stationStatusList.add(stationStatus1);
			//计算组装工位每个产品的生产状况
			StationStatus stationStatus2 = new StationStatus();
			stationStatus2.setStationName(stationName[1]);
			List<ProductStatus> productStatusList2 = new ArrayList<>();
			ProductStatus productStatus21 = new ProductStatus();
			productStatus21.setPname("0960外压组件");
			int zz1 = 0;
			for (int i = 0; i < zzList.size(); i++) {
				Assemble assemble = zzList.get(i);
				if ("0960".equals(assemble.getAtube().substring(3, 7))) {
					zz1 ++;
				}
			}
			if ("".equals(b1) || b1 == null) {
				productStatus21.setPlanAmount(0);
				productStatus21.setLastPlanAmount(0 - zz1);
				productStatus21.setProductivity(0+"");
			} else {
				productStatus21.setPlanAmount(Integer.parseInt(b1));
				productStatus21.setLastPlanAmount(Integer.parseInt(b1) - zz1/24);
				productStatus21.setProductivity(decimalFormat.format((double)zz1*100/24/Integer.parseInt(b1))+"%");
			}
			productStatus21.setCompleteAmount(zz1/24);
			productStatusList2.add(productStatus21);
			ProductStatus productStatus22 = new ProductStatus();
			productStatus22.setPname("0980外压组件");
			int zz2 = 0;
			for (int i = 0; i < zzList.size(); i++) {
				Assemble assemble = zzList.get(i);
				if ("0980".equals(assemble.getAtube().substring(3, 7))) {
					zz2 ++;
				}
			}
			if ("".equals(c1) || c1 == null) {
				productStatus22.setPlanAmount(0);
				productStatus22.setLastPlanAmount(0 - zz2);
				productStatus22.setProductivity(0+"");
			} else {
				productStatus22.setPlanAmount(Integer.parseInt(c1));
				productStatus22.setLastPlanAmount(Integer.parseInt(c1) - zz2/24);
				productStatus22.setProductivity(decimalFormat.format((double)zz2*100/24/Integer.parseInt(c1))+"%");
			}
			productStatus22.setCompleteAmount(zz2/24);
			productStatusList2.add(productStatus22);
			ProductStatus productStatus23 = new ProductStatus();
			productStatus23.setPname("1080组件");
			int zz3 = 0;
			for (int i = 0; i < zzList.size(); i++) {
				Assemble assemble = zzList.get(i);
				if ("1080".equals(assemble.getAtube().substring(3, 7))) {
					zz3 ++;
				}
			}
			if ("".equals(d1) || d1 == null) {
				productStatus23.setPlanAmount(0);
				productStatus23.setLastPlanAmount(0 - zz3);
				productStatus23.setProductivity(0+"");
			} else {
				productStatus23.setPlanAmount(Integer.parseInt(d1));
				productStatus23.setLastPlanAmount(Integer.parseInt(d1) - zz3/32);
				productStatus23.setProductivity(decimalFormat.format((double)zz3*100/32/Integer.parseInt(d1))+"%");
			}
			productStatus23.setCompleteAmount(zz3/32);
			productStatusList2.add(productStatus23);
			ProductStatus productStatus24 = new ProductStatus();
			productStatus24.setPname("1060组件");
			int zz4 = 0;
			for (int i = 0; i < zzList.size(); i++) {
				Assemble assemble = zzList.get(i);
				if ("1060".equals(assemble.getAtube().substring(3, 7))) {
					zz4 ++;
				}
			}
			if ("".equals(e1) || e1 == null) {
				productStatus24.setPlanAmount(0);
				productStatus24.setLastPlanAmount(0 - zz4);
				productStatus24.setProductivity(0+"");
			} else {
				productStatus24.setPlanAmount(Integer.parseInt(e1));
				productStatus24.setLastPlanAmount(Integer.parseInt(e1) - zz4/32);
				productStatus24.setProductivity(decimalFormat.format((double)zz4*100/32/Integer.parseInt(e1))+"%");
			}
			productStatus24.setCompleteAmount(zz4/32);
			productStatusList2.add(productStatus24);
			stationStatus2.setProductStatusList(productStatusList2);
			stationStatusList.add(stationStatus2);
			//计算离心工位每个产品的生产状况
			StationStatus stationStatus3 = new StationStatus();
			stationStatus3.setStationName(stationName[2]);
			List<ProductStatus> productStatusList3 = new ArrayList<>();
			ProductStatus productStatus31 = new ProductStatus();
			productStatus31.setPname("0960外压组件");
			int lx1 = 0;
			for (int i = 0; i < lxList.size(); i++) {
				Centrifugal centrifugal = lxList.get(i);
				if ("0960".equals(centrifugal.getCtube().substring(3, 7))) {
					lx1 ++;
				}
			}
			if ("".equals(b1) || b1 == null) {
				productStatus31.setPlanAmount(0);
				productStatus31.setLastPlanAmount(0 - lx1);
				productStatus31.setProductivity(0+"");
			} else {
				productStatus31.setPlanAmount(Integer.parseInt(b1));
				productStatus31.setLastPlanAmount(Integer.parseInt(b1) - lx1);
				productStatus31.setProductivity(decimalFormat.format((double)lx1*100/Integer.parseInt(b1))+"%");
			}
			productStatus31.setCompleteAmount(lx1);
			productStatusList3.add(productStatus31);
			ProductStatus productStatus32 = new ProductStatus();
			productStatus32.setPname("0980外压组件");
			int lx2 = 0;
			for (int i = 0; i < lxList.size(); i++) {
				Centrifugal centrifugal = lxList.get(i);
				if ("0980".equals(centrifugal.getCtube().substring(3, 7))) {
					lx2 ++;
				}
			}
			if ("".equals(c1) || c1 == null) {
				productStatus32.setPlanAmount(0);
				productStatus32.setLastPlanAmount(0 - lx2);
				productStatus32.setProductivity(0+"");
			} else {
				productStatus32.setPlanAmount(Integer.parseInt(c1));
				productStatus32.setLastPlanAmount(Integer.parseInt(c1) - lx2);
				productStatus32.setProductivity(decimalFormat.format((double)lx2*100/Integer.parseInt(c1))+"%");
			}
			productStatus32.setCompleteAmount(lx2);
			productStatusList3.add(productStatus32);
			ProductStatus productStatus33 = new ProductStatus();
			productStatus33.setPname("1080组件");
			int lx3 = 0;
			for (int i = 0; i < lxList.size(); i++) {
				Centrifugal centrifugal = lxList.get(i);
				if ("1080".equals(centrifugal.getCtube().substring(3, 7))) {
					lx3 ++;
				}
			}
			if ("".equals(d1) || d1 == null) {
				productStatus33.setPlanAmount(0);
				productStatus33.setLastPlanAmount(0 - lx3);
				productStatus33.setProductivity(0+"");
			} else {
				productStatus33.setPlanAmount(Integer.parseInt(d1));
				productStatus33.setLastPlanAmount(Integer.parseInt(d1) - lx3);
				productStatus33.setProductivity(decimalFormat.format((double)lx3*100/Integer.parseInt(d1))+"%");
			}
			productStatus33.setCompleteAmount(lx3);
			productStatusList3.add(productStatus33);
			ProductStatus productStatus34 = new ProductStatus();
			productStatus34.setPname("1060组件");
			int lx4 = 0;
			for (int i = 0; i < lxList.size(); i++) {
				Centrifugal centrifugal = lxList.get(i);
				if ("1060".equals(centrifugal.getCtube().substring(3, 7))) {
					lx4 ++;
				}
			}
			if ("".equals(e1) || e1 == null) {
				productStatus34.setPlanAmount(0);
				productStatus34.setLastPlanAmount(0 -lx4);
				productStatus34.setProductivity(0+"");
			} else {
				productStatus34.setPlanAmount(Integer.parseInt(e1));
				productStatus34.setLastPlanAmount(Integer.parseInt(e1) - lx4);
				productStatus34.setProductivity(decimalFormat.format((double)lx4*100/Integer.parseInt(e1))+"%");
			}
			productStatus34.setCompleteAmount(lx4);
			productStatusList3.add(productStatus34);
			stationStatus3.setProductStatusList(productStatusList3);
			stationStatusList.add(stationStatus3);
			//计算固化工位每个产品的生产状况
			StationStatus stationStatus4 = new StationStatus();
			stationStatus4.setStationName(stationName[3]);
			List<ProductStatus> productStatusList4 = new ArrayList<>();
			ProductStatus productStatus41 = new ProductStatus();
			productStatus41.setPname("0960外压组件");
			int gh1 = 0;
			for (int i = 0; i < ghList.size(); i++) {
				Solid solid = ghList.get(i);
				if ("0960".equals(solid.getStube().substring(3, 7))) {
					gh1 ++;
				}
			}
			if ("".equals(b1) || b1 == null) {
				productStatus41.setPlanAmount(0);
				productStatus41.setLastPlanAmount(0 - gh1);
				productStatus41.setProductivity(0+"");
			} else {
				productStatus41.setPlanAmount(Integer.parseInt(b1));
				productStatus41.setLastPlanAmount(Integer.parseInt(b1) - gh1);
				productStatus41.setProductivity(decimalFormat.format((double)gh1*100/Integer.parseInt(b1))+"%");
			}
			productStatus41.setCompleteAmount(gh1);
			productStatusList4.add(productStatus41);
			ProductStatus productStatus42 = new ProductStatus();
			productStatus42.setPname("0980外压组件");
			int gh2 = 0;
			for (int i = 0; i < ghList.size(); i++) {
				Solid solid = ghList.get(i);
				if ("0980".equals(solid.getStube().substring(3, 7))) {
					gh2 ++;
				}
			}
			if ("".equals(c1) || c1 == null) {
				productStatus42.setPlanAmount(0);
				productStatus42.setLastPlanAmount(0 - gh2);
				productStatus42.setProductivity(0+"");
			} else {
				productStatus42.setPlanAmount(Integer.parseInt(c1));
				productStatus42.setLastPlanAmount(Integer.parseInt(c1) - gh2);
				productStatus42.setProductivity(decimalFormat.format((double)gh2*100/Integer.parseInt(c1))+"%");
			}
			productStatus42.setCompleteAmount(gh2);
			productStatusList4.add(productStatus42);
			ProductStatus productStatus43 = new ProductStatus();
			productStatus43.setPname("1080组件");
			int gh3 = 0;
			for (int i = 0; i < ghList.size(); i++) {
				Solid solid = ghList.get(i);
				if ("1080".equals(solid.getStube().substring(3, 7))) {
					gh3 ++;
				}
			}
			if ("".equals(d1) || d1 == null) {
				productStatus43.setPlanAmount(0);
				productStatus43.setLastPlanAmount(0 - gh3);
				productStatus43.setProductivity(0+"");
			} else {
				productStatus43.setPlanAmount(Integer.parseInt(d1));
				productStatus43.setLastPlanAmount(Integer.parseInt(d1) - gh3);
				productStatus43.setProductivity(decimalFormat.format((double)gh3*100/Integer.parseInt(d1))+"%");
			}
			productStatus43.setCompleteAmount(gh3);
			productStatusList4.add(productStatus43);
			ProductStatus productStatus44 = new ProductStatus();
			productStatus44.setPname("1060组件");
			int gh4 = 0;
			for (int i = 0; i < ghList.size(); i++) {
				Solid solid = ghList.get(i);
				if ("1060".equals(solid.getStube().substring(3, 7))) {
					gh4 ++;
				}
			}
			if ("".equals(e1) || e1 == null) {
				productStatus44.setPlanAmount(0);
				productStatus44.setLastPlanAmount(0 - gh4);
				productStatus44.setProductivity(0+"");
			} else {
				productStatus44.setPlanAmount(Integer.parseInt(e1));
				productStatus44.setLastPlanAmount(Integer.parseInt(e1) - gh4);
				productStatus44.setProductivity(decimalFormat.format((double)gh4*100/Integer.parseInt(e1))+"%");
			}
			productStatus44.setCompleteAmount(gh4);
			productStatusList4.add(productStatus44);
			stationStatus4.setProductStatusList(productStatusList4);
			stationStatusList.add(stationStatus4);
			//计算切割工位每个产品的生产状况
			StationStatus stationStatus5 = new StationStatus();
			stationStatus5.setStationName(stationName[4]);
			List<ProductStatus> productStatusList5 = new ArrayList<>();
			ProductStatus productStatus51 = new ProductStatus();
			productStatus51.setPname("0960外压组件");
			int qg1 = 0;
			for (int i = 0; i < qgList.size(); i++) {
				Cut cut = qgList.get(i);
				if ("0960".equals(cut.getCtube().substring(3, 7))) {
					qg1 ++;
				}
			}
			if ("".equals(b1) || b1 == null) {
				productStatus51.setPlanAmount(0);
				productStatus51.setLastPlanAmount(0 - qg1);
				productStatus51.setProductivity(0+"");
			} else {
				productStatus51.setPlanAmount(Integer.parseInt(b1));
				productStatus51.setLastPlanAmount(Integer.parseInt(b1) - qg1);
				productStatus51.setProductivity(decimalFormat.format((double)qg1*100/Integer.parseInt(b1))+"%");
			}
			productStatus51.setCompleteAmount(qg1);
			productStatusList5.add(productStatus51);
			ProductStatus productStatus52 = new ProductStatus();
			productStatus52.setPname("0980外压组件");
			int qg2 = 0;
			for (int i = 0; i < qgList.size(); i++) {
				Cut cut = qgList.get(i);
				if ("0980".equals(cut.getCtube().substring(3, 7))) {
					qg2 ++;
				}
			}
			if ("".equals(c1) || c1 == null) {
				productStatus52.setPlanAmount(0);
				productStatus52.setLastPlanAmount(0 - qg2);
				productStatus52.setProductivity(0+"");
			} else {
				productStatus52.setPlanAmount(Integer.parseInt(c1));
				productStatus52.setLastPlanAmount(Integer.parseInt(c1) - qg2);
				productStatus52.setProductivity(decimalFormat.format((double)qg2*100/Integer.parseInt(c1))+"%");
			}
			productStatus52.setCompleteAmount(qg2);
			productStatusList5.add(productStatus52);
			ProductStatus productStatus53 = new ProductStatus();
			productStatus53.setPname("1080组件");
			int qg3 = 0;
			for (int i = 0; i < qgList.size(); i++) {
				Cut cut = qgList.get(i);
				if ("1080".equals(cut.getCtube().substring(3, 7))) {
					qg3 ++;
				}
			}
			if ("".equals(d1) || d1 == null) {
				productStatus53.setPlanAmount(0);
				productStatus53.setLastPlanAmount(0 - qg3);
				productStatus53.setProductivity(0+"");
			} else {
				productStatus53.setPlanAmount(Integer.parseInt(d1));
				productStatus53.setLastPlanAmount(Integer.parseInt(d1) - qg3);
				productStatus53.setProductivity(decimalFormat.format((double)qg3*100/Integer.parseInt(d1))+"%");
			}
			productStatus53.setCompleteAmount(qg3);
			productStatusList5.add(productStatus53);
			ProductStatus productStatus54 = new ProductStatus();
			productStatus54.setPname("1060组件");
			int qg4 = 0;
			for (int i = 0; i < qgList.size(); i++) {
				Cut cut = qgList.get(i);
				if ("1060".equals(cut.getCtube().substring(3, 7))) {
					qg4 ++;
				}
			}
			if ("".equals(e1) || e1 == null) {
				productStatus54.setPlanAmount(0);
				productStatus54.setLastPlanAmount(0 - qg4);
				productStatus54.setProductivity(0+"");
			} else {
				productStatus54.setPlanAmount(Integer.parseInt(e1));
				productStatus54.setLastPlanAmount(Integer.parseInt(e1) - qg4);
				productStatus54.setProductivity(decimalFormat.format((double)qg4*100/Integer.parseInt(e1))+"%");
			}
			productStatus54.setCompleteAmount(qg4);
			productStatusList5.add(productStatus54);
			stationStatus5.setProductStatusList(productStatusList5);
			stationStatusList.add(stationStatus5);
			return stationStatusList;
		} else {
			//排产单不存在
			return stationStatusList;
		}
	}

	/**
	 * 查询产量
	 */
	public List<ProductionStatistics> selectOutputByTime(String time) {
		// TODO Auto-generated method stub
		try {
			List<ProductionStatistics> productionStatisticsList = new ArrayList<>();
			ProductionStatistics productionStatistics = new ProductionStatistics();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Map<String, Object> map1 = new HashMap<>();
			Map<String, Object> map2 = new HashMap<>();
			if (null == time || "".equals(time)) {
				Date d  = new Date();
				int days = getDaysOfMonth(d);
				map1.put("beginTime", sdf.format(d) + "-01 00:00:00");
				map1.put("endTime", sdf.format(d) + "-" + days + " 23:59:59");
				map2.put("beginTime", sdf.format(d).split("-")[0] + "-01-01 00:00:00");
				map2.put("endTime", sdf.format(d).split("-")[0] + "-12-31 23:59:59");
				productionStatistics.setYears(sdf.format(d).split("-")[0]);
				productionStatistics.setMonth(sdf.format(d).split("-")[1]);
			} else {
				Date d  = null;
				d  = sdf.parse(time);
				int days = getDaysOfMonth(d);
				map1.put("beginTime", time + "-01 00:00:00");
				map1.put("endTime", time + "-" + days + " 23:59:59");
				map2.put("beginTime", time.split("-")[0] + "-01-01 00:00:00");
				map2.put("endTime", time.split("-")[0] + "-12-31 23:59:59");
				productionStatistics.setYears(time.split("-")[0]);
				productionStatistics.setMonth(time.split("-")[1]);
			}
			int yearOutput = allReportMapper.selectOutputByTime(map2);
			int monthOutput = allReportMapper.selectOutputByTime(map1);
			productionStatistics.setYearOutput(yearOutput);
			productionStatistics.setMonthOutput(monthOutput);
			productionStatisticsList.add(productionStatistics);
			return productionStatisticsList;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取指定月的天数
	 * @param date
	 * @return
	 */
	public static int getDaysOfMonth(Date date) {
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 查询产量详情
	 * @param time
	 * @param pnum
	 * @return
	 */
	public List<ProductionStatisticsDetail> listPagePLDetailByTime(String time,String pnum) {
		try {
			Map<String, Object> map1 = new HashMap<>();
			Map<String, Object> map2 = new HashMap<>();
			List<ProductionStatisticsDetail> pLDetailList = new ArrayList<>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date d  = null;
			d  = sdf.parse(time);
			int days = getDaysOfMonth(d);
			map1.put("beginTime", time + "-01");
			map1.put("endTime", time + "-" + days);
			map1.put("pnum", pnum);
			map2.put("beginTime", time + "-01 00:00:00");
			map2.put("endTime", time + "-" + days + " 23:59:59");
			map2.put("pnum", pnum);
			List<Shedudetail> shedudetailList = allReportMapper.listAllShedudetailByTime(map1);
			List<ProductionStatisticsDetail> pStatisticsDetails = allReportMapper.listAllPLDetailByTime(map2);
			for (Shedudetail shedudetail : shedudetailList) {
				String b1 = shedudetail.getB1();
				String c1 = shedudetail.getC1();
				String d1 = shedudetail.getD1();
				String e1 = shedudetail.getE1();
				if ("PSH2-0960-T-01".equals(pnum)) {
					if (null != b1 &&  !"".equals(b1)) {
						ProductionStatisticsDetail pLDetail = new ProductionStatisticsDetail();
						pLDetail.setpDate(shedudetail.getRemark1());
						pLDetail.setPnum(pnum);
						pLDetail.setPdOutput(Integer.parseInt(b1));
						if (null != pStatisticsDetails && pStatisticsDetails.size() >0) {
							for (ProductionStatisticsDetail productionStatisticsDetail : pStatisticsDetails) {
								if (shedudetail.getRemark1().equals(productionStatisticsDetail.getpDate())) {
									pLDetail.setAdOutput(productionStatisticsDetail.getAdOutput());
									int difference = productionStatisticsDetail.getAdOutput() - Integer.parseInt(b1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(b1)));
									break;
								} else {
									pLDetail.setAdOutput(0);
									int difference = 0 - Integer.parseInt(b1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(b1)));
								}
							}
						} else {
							pLDetail.setAdOutput(0);
							int difference = 0 - Integer.parseInt(b1);
							pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(b1)));
						}
						pLDetailList.add(pLDetail);
					}
				}
				if ("PSH2-0980-T-02".equals(pnum)) {
					if (null != c1 &&  !"".equals(c1)) {
						ProductionStatisticsDetail pLDetail = new ProductionStatisticsDetail();
						pLDetail.setpDate(shedudetail.getRemark1());
						pLDetail.setPnum(pnum);
						pLDetail.setPdOutput(Integer.parseInt(c1));
						if (null != pStatisticsDetails && pStatisticsDetails.size() >0) {
							for (ProductionStatisticsDetail productionStatisticsDetail : pStatisticsDetails) {
								if (shedudetail.getRemark1().equals(productionStatisticsDetail.getpDate())) {
									pLDetail.setAdOutput(productionStatisticsDetail.getAdOutput());
									int difference = productionStatisticsDetail.getAdOutput() - Integer.parseInt(c1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(c1)));
									break;
								} else {
									pLDetail.setAdOutput(0);
									int difference = 0 - Integer.parseInt(c1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(c1)));
								}
							}
						} else {
							pLDetail.setAdOutput(0);
							int difference = 0 - Integer.parseInt(c1);
							pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(c1)));
						}
						pLDetailList.add(pLDetail);
					}
				}
				if ("PSH1-1060-B-03".equals(pnum)) {
					if (null != e1 &&  !"".equals(e1)) {
						ProductionStatisticsDetail pLDetail = new ProductionStatisticsDetail();
						pLDetail.setpDate(shedudetail.getRemark1());
						pLDetail.setPnum(pnum);
						pLDetail.setPdOutput(Integer.parseInt(e1));
						if (null != pStatisticsDetails && pStatisticsDetails.size() >0) {
							for (ProductionStatisticsDetail productionStatisticsDetail : pStatisticsDetails) {
								if (shedudetail.getRemark1().equals(productionStatisticsDetail.getpDate())) {
									pLDetail.setAdOutput(productionStatisticsDetail.getAdOutput());
									int difference = productionStatisticsDetail.getAdOutput() - Integer.parseInt(e1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(e1)));
									break;
								} else {
									pLDetail.setAdOutput(0);
									int difference = 0 - Integer.parseInt(e1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(e1)));
								}
							}
						} else {
							pLDetail.setAdOutput(0);
							int difference = 0 - Integer.parseInt(e1);
							pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(e1)));
						}
						pLDetailList.add(pLDetail);
					}
				}
				if ("PSH1-1080-C-04".equals(pnum)) {
					if (null != d1 && !"".equals(d1)) {
						ProductionStatisticsDetail pLDetail = new ProductionStatisticsDetail();
						pLDetail.setpDate(shedudetail.getRemark1());
						pLDetail.setPnum(pnum);
						pLDetail.setPdOutput(Integer.parseInt(d1));
						if (null != pStatisticsDetails && pStatisticsDetails.size() >0) {
							for (ProductionStatisticsDetail productionStatisticsDetail : pStatisticsDetails) {
								if (shedudetail.getRemark1().equals(productionStatisticsDetail.getpDate())) {
									pLDetail.setAdOutput(productionStatisticsDetail.getAdOutput());
									int difference = productionStatisticsDetail.getAdOutput() - Integer.parseInt(d1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(d1)));
									break;
								} else {
									pLDetail.setAdOutput(0);
									int difference = 0 - Integer.parseInt(d1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(d1)));
								}
							}
						} else {
							pLDetail.setAdOutput(0);
							int difference = 0 - Integer.parseInt(d1);
							pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(d1)));
						}
						pLDetailList.add(pLDetail);
					}
				}
			}
			return pLDetailList;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 查询产量详情总数
	 * @param map
	 * @param time
	 * @return
	 */
	public Integer countPLDetailByTime(Map<String, Object> map,String time) {
		// TODO Auto-generated method stub
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date d  = null;
			d  = sdf.parse(time);
			int days = getDaysOfMonth(d);
			map.put("beginTime", time + "-01 00:00:00");
			map.put("endTime", time + "-" + days + " 23:59:59");
			return allReportMapper.countPLDetailByTime(map);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getPercentage(int a, int b) {
		NumberFormat nt = NumberFormat.getPercentInstance();
		nt.setMinimumFractionDigits(2);
		double value = (double)a/b;
		return nt.format(value);
	}

	@Override
	public List<ProductionStatisticsDetail> selectAndroidOutputByTime(String time, String pnum) {
		// TODO Auto-generated method stub
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Map<String, Object> map1 = new HashMap<>();
			Map<String, Object> map2 = new HashMap<>();
			String years = "";
			String month = "";
			if (null == time || "".equals(time)) {
				Date d  = new Date();
				int days = getDaysOfMonth(d);
				map1.put("beginTime", sdf.format(d) + "-01 00:00:00");
				map1.put("endTime", sdf.format(d) + "-" + days + " 23:59:59");
				map2.put("beginTime", sdf.format(d).split("-")[0] + "-01-01 00:00:00");
				map2.put("endTime", sdf.format(d).split("-")[0] + "-12-31 23:59:59");
				years = sdf.format(d).split("-")[0];
				month = sdf.format(d).split("-")[1];
			} else {
				Date d  = null;
				d  = sdf.parse(time);
				int days = getDaysOfMonth(d);
				map1.put("beginTime", time + "-01 00:00:00");
				map1.put("endTime", time + "-" + days + " 23:59:59");
				map2.put("beginTime", time.split("-")[0] + "-01-01 00:00:00");
				map2.put("endTime", time.split("-")[0] + "-12-31 23:59:59");
				years = time.split("-")[0];
				month = time.split("-")[1];
			}
			map1.put("pnum", pnum);
			map2.put("pnum", pnum);
			int yearOutput = allReportMapper.selectOutputByTimeToAndroid(map2);
			int monthOutput = allReportMapper.selectOutputByTimeToAndroid(map1);
			Map<String, Object> map3 = new HashMap<>();
			Map<String, Object> map4 = new HashMap<>();
			List<ProductionStatisticsDetail> pLDetailList = new ArrayList<>();
			Date d  = null;
			d  = sdf.parse(time);
			int days = getDaysOfMonth(d);
			map3.put("beginTime", time + "-01");
			map3.put("endTime", time + "-" + days);
			map3.put("pnum", pnum);
			map4.put("beginTime", time + "-01 00:00:00");
			map4.put("endTime", time + "-" + days + " 23:59:59");
			map4.put("pnum", pnum);
			List<Shedudetail> shedudetailList = allReportMapper.listAllShedudetailByTime(map3);
			List<ProductionStatisticsDetail> pStatisticsDetails = allReportMapper.listAllPLDetailByTime(map4);
			for (Shedudetail shedudetail : shedudetailList) {
				String b1 = shedudetail.getB1();
				String c1 = shedudetail.getC1();
				String d1 = shedudetail.getD1();
				String e1 = shedudetail.getE1();
				if ("PSH2-0960-T-01".equals(pnum)) {
					if (!"".equals(b1)) {
						ProductionStatisticsDetail pLDetail = new ProductionStatisticsDetail();
						pLDetail.setpDate(shedudetail.getRemark1());
						pLDetail.setPnum(pnum);
						pLDetail.setPdOutput(Integer.parseInt(b1));
						pLDetail.setYears(years);
						pLDetail.setMonth(month);
						pLDetail.setYearOutput(yearOutput);
						pLDetail.setMonthOutput(monthOutput);
						if (null != pStatisticsDetails && pStatisticsDetails.size() >0) {
							for (ProductionStatisticsDetail productionStatisticsDetail : pStatisticsDetails) {
								if (shedudetail.getRemark1().equals(productionStatisticsDetail.getpDate())) {
									pLDetail.setAdOutput(productionStatisticsDetail.getAdOutput());
									int difference = productionStatisticsDetail.getAdOutput() - Integer.parseInt(b1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(b1)));
								} else {
									pLDetail.setAdOutput(0);
									int difference = 0 - Integer.parseInt(b1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(b1)));
								}
							}
						} else {
							pLDetail.setAdOutput(0);
							int difference = 0 - Integer.parseInt(b1);
							pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(b1)));
						}
						pLDetailList.add(pLDetail);
					}
				}
				if ("PSH2-0980-T-02".equals(pnum)) {
					if (!"".equals(c1)) {
						ProductionStatisticsDetail pLDetail = new ProductionStatisticsDetail();
						pLDetail.setpDate(shedudetail.getRemark1());
						pLDetail.setPnum(pnum);
						pLDetail.setPdOutput(Integer.parseInt(c1));
						pLDetail.setYears(years);
						pLDetail.setMonth(month);
						pLDetail.setYearOutput(yearOutput);
						pLDetail.setMonthOutput(monthOutput);
						if (null != pStatisticsDetails && pStatisticsDetails.size() >0) {
							for (ProductionStatisticsDetail productionStatisticsDetail : pStatisticsDetails) {
								if (shedudetail.getRemark1().equals(productionStatisticsDetail.getpDate())) {
									pLDetail.setAdOutput(productionStatisticsDetail.getAdOutput());
									int difference = productionStatisticsDetail.getAdOutput() - Integer.parseInt(c1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(c1)));
								} else {
									pLDetail.setAdOutput(0);
									int difference = 0 - Integer.parseInt(c1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(c1)));
								}
							}
						} else {
							pLDetail.setAdOutput(0);
							int difference = 0 - Integer.parseInt(c1);
							pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(c1)));
						}
						pLDetailList.add(pLDetail);
					}
				}
				if ("PSH1-1060-B-03".equals(pnum)) {
					if (!"".equals(e1)) {
						ProductionStatisticsDetail pLDetail = new ProductionStatisticsDetail();
						pLDetail.setpDate(shedudetail.getRemark1());
						pLDetail.setPnum(pnum);
						pLDetail.setPdOutput(Integer.parseInt(e1));
						pLDetail.setYears(years);
						pLDetail.setMonth(month);
						pLDetail.setYearOutput(yearOutput);
						pLDetail.setMonthOutput(monthOutput);
						if (null != pStatisticsDetails && pStatisticsDetails.size() >0) {
							for (ProductionStatisticsDetail productionStatisticsDetail : pStatisticsDetails) {
								if (shedudetail.getRemark1().equals(productionStatisticsDetail.getpDate())) {
									pLDetail.setAdOutput(productionStatisticsDetail.getAdOutput());
									int difference = productionStatisticsDetail.getAdOutput() - Integer.parseInt(e1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(e1)));
								} else {
									pLDetail.setAdOutput(0);
									int difference = 0 - Integer.parseInt(e1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(e1)));
								}
							}
						} else {
							pLDetail.setAdOutput(0);
							int difference = 0 - Integer.parseInt(e1);
							pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(e1)));
						}
						pLDetailList.add(pLDetail);
					}
				}
				if ("PSH1-1080-C-04".equals(pnum)) {
					if (!"".equals(d1)) {
						ProductionStatisticsDetail pLDetail = new ProductionStatisticsDetail();
						pLDetail.setpDate(shedudetail.getRemark1());
						pLDetail.setPnum(pnum);
						pLDetail.setPdOutput(Integer.parseInt(d1));
						pLDetail.setYears(years);
						pLDetail.setMonth(month);
						pLDetail.setYearOutput(yearOutput);
						pLDetail.setMonthOutput(monthOutput);
						if (null != pStatisticsDetails && pStatisticsDetails.size() >0) {
							for (ProductionStatisticsDetail productionStatisticsDetail : pStatisticsDetails) {
								if (shedudetail.getRemark1().equals(productionStatisticsDetail.getpDate())) {
									pLDetail.setAdOutput(productionStatisticsDetail.getAdOutput());
									int difference = productionStatisticsDetail.getAdOutput() - Integer.parseInt(d1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(d1)));
								} else {
									pLDetail.setAdOutput(0);
									int difference = 0 - Integer.parseInt(d1);
									pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(d1)));
								}
							}
						} else {
							pLDetail.setAdOutput(0);
							int difference = 0 - Integer.parseInt(d1);
							pLDetail.setdRate(getPercentage(Math.abs(difference),Integer.parseInt(d1)));
						}
						pLDetailList.add(pLDetail);
					}
				}
			}
			return pLDetailList;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Integer selectMSQuantity(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return allReportMapper.selectMSQuantity(map);
	}
}
